﻿using SerieSpotter.Filters;
using SerieSpotter.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using System.Data.Entity;

namespace SerieSpotter.Controllers
{
    // ook hier kan authorize worden geplaats geldt dan voor heel de controller
    [Log]
    public class SeriesController : LanguageController
    {

        SerieSpotterDb _db = new SerieSpotterDb();

        public ActionResult Autocomplete(string term)
        {

            var model = _db.Shows
               .Where(r => term == null || r.Title.StartsWith(term))
                .Select(r => new
                {
                    label = r.Title
                });

            return Json(model, JsonRequestBehavior.AllowGet);
        }



       //paging nog doen
        public ActionResult Index(string searchTerm = null)
        {
            //var model = _db.Shows.ToList(); //haalt alle shows uit de database
            var model = _db.Shows
                .Where(r => searchTerm == null || r.Title.Contains(searchTerm));

            if (Request.IsAjaxRequest())
            {
                return PartialView("_Series", model);
            }

            return View(model);
           
        }


       

        public ActionResult Search(string name="default")
        {
            var message = Server.HtmlEncode(name);
            return Content(message);
        }


    
        //
        // GET: /Series/Create
        [HttpGet]
        // [Authorize(Roles = "admin")]
        [Authorize(Roles = "admin")] // beide mogen een comment creeëren
        public ActionResult Create()
        {
                return View();
        }
        //
        // POST: /Series/Create
        [HttpPost]
        // [Authorize(Roles="admin")]
        [Authorize(Roles = "admin")]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Show show)
        {
            if (ModelState.IsValid)
            {
                _db.Shows.Add(show); // adden
                _db.SaveChanges(); // saven in db
                return RedirectToAction("Index"); // terug naar de overzichtspagina
            }
            return View(show);
        }

        //
        // GET: /Series/Edit/5

        [HttpGet]
        [Authorize(Roles = "admin")]
        public ActionResult Edit(int id)
        {
            var model = _db.Shows.Find(id); // haal gegevens op
            return View(model); // toon op scherm
        }

        //
        // POST: /Series/Edit/5

        [HttpPost]
        [Authorize(Roles = "admin")]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Show show)
        {
            if (ModelState.IsValid)
            {

                _db.Entry(show).State = EntityState.Modified; // er is iets veranderd!
                _db.SaveChanges(); // opslaan veranderingen
                return RedirectToAction("Index"); //redirect
            }
            return View(show);
        }
        //
        // GET: /Series/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Series/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        
        //sluit de database connectie
        protected override void Dispose(bool disposing)
        {
            if (_db != null)
                _db.Dispose();
            base.Dispose(disposing);
        }
         
    }
}